package main.interaction
{
	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	
	[Event(type="main.StateEvent", name="regionOver")]
	[Event(type="main.StateEvent", name="regionOut")]
	[Event(type="main.StateEvent", name="regionUp")]
	public class Region extends Sprite
	{
		
		public var id:String;
		
		public var label:String;
		
		private var shape:Shape = new Shape();
		
		private var _enabled:Boolean = false;
		
		[Embed(source="assets/regionHotspot.png")]
		private static var RegionHotspot:Class;
		private var hotspot:Bitmap = new RegionHotspot();
		
		public function Region(id:String)
		{
			addChild(hotspot);
			hotspot.alpha = 0;
			hotspot.blendMode = BlendMode.ADD;
			
			addChild(shape);
			this.id = id;
			shape.alpha = .2;
			
			enabled = false;
		}
		
		public function set enabled(b:Boolean):void {
			if (b) {
				addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
				addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			} else {
				removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
				removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			}
			useHandCursor = b;
			buttonMode = b;
			mouseEnabled = b;
			visible = b;
			_enabled = b;
		}
		
		public function get enabled():Boolean {
			return _enabled;
		}
		
		private function onMouseOver(e:MouseEvent):void {
			shape.alpha = .5;
			TweenLite.to(hotspot, .5, {
				alpha: .5
			});
		}
		
		private function onMouseOut(e:MouseEvent=null):void {
			shape.alpha = .2;
			TweenLite.to(hotspot, .5, {
				alpha: 0
			});
		}
		
		private function onMouseUp(e:MouseEvent):void {
			onMouseOut();
		}
		
		public function setSize(w:Number, h:Number, l:Number, r:Number):void {
			var g:Graphics = shape.graphics;
			g.clear();
			g.beginFill(0xFF0000,0);
			g.moveTo(0,0);
			g.lineTo(l,h);
			g.lineTo(w-r,h);
			g.lineTo(w,0);
			g.lineTo(0,0);
			g.endFill();
			hotspot.width = w;
			hotspot.height = h*1.5;
		}

	}
}